package me.zhengjie.timing.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.pojo.dto.AgvReturnDto;
import me.zhengjie.pojo.dto.Extra;
import me.zhengjie.pojo.dto.Values;
import me.zhengjie.pojo.entity.WcsDeliveryMission;
import me.zhengjie.pojo.entity.WcsInboundMission;
import me.zhengjie.pojo.entity.WcsOutboundMission;
import me.zhengjie.service.AgvReturnService;
import me.zhengjie.sqlservice.WcsDeliveryMissionService;
import me.zhengjie.sqlservice.WcsInboundMissionService;
import me.zhengjie.sqlservice.WcsOutboundMissionService;
import me.zhengjie.utility.state.TaskStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class TimingTest {
    private static final Logger haikang = LoggerFactory.getLogger("haiKang");

    private final AgvReturnService agvReturnService;

    private final WcsInboundMissionService wcsInboundMissionService;

    private final WcsOutboundMissionService wcsOutboundMissionService;

    private final WcsDeliveryMissionService wcsDeliveryMissionService;


    @Transactional
//    @Scheduled(fixedDelay = 1000)
    public void timingTest(){
        //读取plc地址位，是否能放货

//        Integer oneRequestPutStatus = outboundLineBody.readHoldingRegisters(10, 1);
//        boolean b = outboundLineBody.writeWithRetry(10, 1, 1, 3);
//        System.out.println("plc第一段是否能放货"+oneRequestPutStatus +"写入" +b);

        try {
            haikang.info("任务定时打印");
            List<WcsInboundMission> wcsInboundMissionList = wcsInboundMissionService.selectStatus(TaskStatus.task_send.getStatus());
            List<WcsInboundMission> wcsInboundMissionList2 = wcsInboundMissionService.selectStatus(TaskStatus.task_take.getStatus());
            List<WcsOutboundMission> wcsOutboundMissionList = wcsOutboundMissionService.selectStatus(TaskStatus.task_send.getStatus());
            List<WcsOutboundMission> wcsOutboundMissionList2 = wcsOutboundMissionService.selectStatus(TaskStatus.task_take.getStatus());
            List<WcsOutboundMission> wcsOutboundMissionList3 = wcsOutboundMissionService.selectStatus(TaskStatus.Task_create.getStatus());
            List<WcsDeliveryMission> wcsDeliveryMissionList = wcsDeliveryMissionService.selectMissionStatus(TaskStatus.task_send.getStatus());
            List<WcsDeliveryMission> wcsDeliveryMissionLists = wcsDeliveryMissionService.selectMissionStatus(TaskStatus.task_take.getStatus());
            if (!wcsInboundMissionList.isEmpty()) {
                for (WcsInboundMission wcsInboundMission : wcsInboundMissionList) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsInboundMission.getInboundCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("outbin");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }
            if (!wcsInboundMissionList2.isEmpty()) {
                for (WcsInboundMission wcsInboundMission : wcsInboundMissionList2) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsInboundMission.getInboundCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("end");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }
            if (!wcsOutboundMissionList.isEmpty()) {
                for (WcsOutboundMission wcsOutboundMission : wcsOutboundMissionList) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsOutboundMission.getOutboundCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("outbin");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }

            if (!wcsOutboundMissionList3.isEmpty()) {
                for (WcsOutboundMission wcsOutboundMission : wcsOutboundMissionList3) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsOutboundMission.getOutboundCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("outbin");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }

            if (!wcsOutboundMissionList2.isEmpty()) {
                for (WcsOutboundMission wcsOutboundMission : wcsOutboundMissionList2) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsOutboundMission.getOutboundCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("end");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }

            if (!wcsDeliveryMissionList.isEmpty()) {
                for (WcsDeliveryMission wcsInboundMission : wcsDeliveryMissionList) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsInboundMission.getDeliveryCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("outbin");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }
            if (!wcsDeliveryMissionLists.isEmpty()) {
                for (WcsDeliveryMission wcsInboundMission : wcsDeliveryMissionLists) {
                    AgvReturnDto agvReturnDto = new AgvReturnDto();
                    agvReturnDto.setSingleRobotCode("测试agv");
                    agvReturnDto.setRobotTaskCode(wcsInboundMission.getDeliveryCode());
                    Extra extra = new Extra();
                    Values values = new Values();
                    values.setMethod("end");
                    extra.setValues(values);
                    agvReturnDto.setExtra(extra);
                    agvReturnService.agvTaskReport(agvReturnDto);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
